﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace TFSWitAdmin
{


  public partial class Form1 : Form
  {
    TeamProjectPicker teamProjectPicker = new TeamProjectPicker();

    public Form1()
    {
      InitializeComponent();
    }



    private void cmdConnect_Click(object sender, EventArgs e)
    {
      lblServerCollection.Text = string.Empty;
      lvwWorkItems.Items.Clear();
      
      teamProjectPicker.ShowDialog();
      lblServerCollection.Text = teamProjectPicker.SelectedTeamProjectCollection.Name + @"\" + teamProjectPicker.SelectedProjects[0].Name;

      ShowWorkItems();
    }

    private void ShowWorkItems()
    {
      lblMsg.Text = "Get WorkItem List";
      Application.DoEvents();

      lvwWorkItems.Items.Clear();

      var tp = teamProjectPicker.SelectedProjects[0];
      var tpc = teamProjectPicker.SelectedTeamProjectCollection;

      var workItemStore = tpc.GetService<WorkItemStore>();
      var teamProject = workItemStore.Projects[tp.Name];

      var wic = GetChildWorkItems(workItemStore, tp);


      foreach (var workItem in wic.Cast<WorkItem>())
      {
        var li = new ListViewItem {Text = workItem.Id.ToString(CultureInfo.InvariantCulture)};
        li.SubItems.Add(workItem.Title);
        li.SubItems.Add(workItem.Type.Name);
        li.Tag = workItem;
        lvwWorkItems.Items.Add(li);
      }
      lblMsg.Text = string.Empty;
    }

    private WorkItemCollection GetChildWorkItems(WorkItemStore wiStore,Microsoft.TeamFoundation.Server.ProjectInfo tp)
    {
      var queryString = "Select [State], [Title] From WorkItems Where [Team Project] = '" + tp.Name + "'";
      var query = new Query(wiStore, queryString);
      var results = query.RunQuery();
      return results;
    }

    private void cmdDelete_Click(object sender, EventArgs e)
    {
      if (lvwWorkItems.CheckedItems.Count == 0) return;
      var pg = Environment.GetEnvironmentVariable("ProgramFiles");
      //if (Environment.Is64BitOperatingSystem)
      //  pg += " (x86)";
      var path = pg + @"\Microsoft Visual Studio 10.0\Common7\IDE\";

      var totalWI = lvwWorkItems.CheckedItems.Count;
      var i = 0;
      foreach (var checkedItem in lvwWorkItems.CheckedItems)
      {
        i++;
        var wi = (WorkItem)((ListViewItem) checkedItem).Tag;
        var cmd = @" destroywi /collection:" +
                  teamProjectPicker.SelectedTeamProjectCollection.ConfigurationServer.Uri.AbsoluteUri.Replace("/",@"\");
        cmd += @"\GLC /id:" + wi.Id.ToString(CultureInfo.InvariantCulture) + @" /noprompt";

        string ret = string.Empty;
        string errMsg = string.Empty;

        lblMsg.Text = string.Format("Deleting WorkItem Id: {0} ({1}/{2})", wi.Id.ToString(CultureInfo.InvariantCulture), totalWI.ToString(CultureInfo.InvariantCulture) , i.ToString(CultureInfo.InvariantCulture));
        lblMsg.Refresh();
        Application.DoEvents();
        Generals.ExecuteShellCommand(path + "witadmin.exe", cmd, ref ret, ref errMsg);

        ShowWorkItems();

      }
      lblMsg.Text = string.Empty;





    }


  }
}
